//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "PaymentTypeBridge.h"
#include "StringUtilities.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)

TPaymentType :: TPaymentType()
{
}

TPaymentType :: TPaymentType( TPaymentType* other)
{
   id          = other->id;
   description = other->description;
}

//-------------------------- Invoice Bridge ----------------------------------

TPaymentTypeBridge :: TPaymentTypeBridge( TDatabase * database)
{
   qryGet = new TQuery( NULL);
   qryGet->DatabaseName = database->DatabaseName;
   BuildGetQuery();

   qryAdd = new TQuery( NULL);
   qryAdd->DatabaseName = database->DatabaseName;
   BuildAddQuery();

   qryUpdate = new TQuery( NULL);
   qryUpdate->DatabaseName = database->DatabaseName;
   BuildUpdateQuery();

   qryDelete = new TQuery( NULL);
   qryDelete->DatabaseName = database->DatabaseName;
   BuildDeleteQuery();
               
   qryGetPaymentTypes = new TQuery( NULL);
   qryGetPaymentTypes->DatabaseName = database->DatabaseName;
   BuildGetPaymentTypesQuery();

   qryInUse = new TQuery( NULL);
   qryInUse->DatabaseName = database->DatabaseName;
   BuildInUseQuery();
}

TPaymentTypeBridge :: ~TPaymentTypeBridge()
{
   delete qryInUse;
   delete qryGetPaymentTypes;
   delete qryDelete;
   delete qryUpdate;
   delete qryAdd;
   delete qryGet;
}

void TPaymentTypeBridge :: BuildGetQuery()
{
   qryGet->SQL->Add( "SELECT * FROM PAYMENTTYPES");
   qryGet->SQL->Add( "WHERE (ID = :prmID)");
}

void TPaymentTypeBridge :: BuildAddQuery()
{
   qryAdd->SQL->Add( "INSERT INTO PAYMENTTYPES (DESCRIPTION) VALUES (:prmDescription)");
}

void TPaymentTypeBridge :: BuildUpdateQuery()
{
   qryUpdate->SQL->Add( "UPDATE PAYMENTTYPES");
   qryUpdate->SQL->Add( "SET DESCRIPTION = :prmDescription");
   qryUpdate->SQL->Add( "WHERE (ID = :prmID)");
}

void TPaymentTypeBridge :: BuildDeleteQuery()
{
   qryDelete->SQL->Add( "DELETE FROM PAYMENTTYPES WHERE");
   qryDelete->SQL->Add( "(ID = :prmID)");
}

void TPaymentTypeBridge :: BuildGetPaymentTypesQuery()
{
   qryGetPaymentTypes->SQL->Add( "SELECT * FROM PAYMENTTYPES");
   qryGetPaymentTypes->SQL->Add( "ORDER BY DESCRIPTION");
}

void TPaymentTypeBridge :: BuildInUseQuery()
{
   qryInUse->SQL->Add( "SELECT * FROM INVOICES");
   qryInUse->SQL->Add( "WHERE PAYMENTTYPE = :prmPaymentType");
}

void SetPaymentType( TQuery* query, TPaymentType* paymentType)
{
   paymentType->id          = query->FieldByName( "ID")->AsInteger;
   paymentType->description = query->FieldByName( "DESCRIPTION")->AsString;
}

void TPaymentTypeBridge :: Get( __int64 id, TPaymentType* paymentType)
{
   qryGet->ParamByName("prmID")->AsInteger = id;

   qryGet->Open();

   try
   {
      SetPaymentType( qryGet, paymentType);
   }
   __finally
   {
      qryGet->Close();
   }
}

void TPaymentTypeBridge :: GetPaymentTypes( TStrings * paymentTypes)
{
   TPaymentType * pt;
   
   qryGetPaymentTypes->Open();

   try
   {
      TStringUtilites :: Clear( paymentTypes);

      qryGetPaymentTypes->First();

      while ( !qryGetPaymentTypes->Eof)
      {
         pt = new TPaymentType();
         SetPaymentType( qryGetPaymentTypes, pt);

         paymentTypes->AddObject( pt->description, pt);
         qryGetPaymentTypes->Next();
      }
   }
   __finally
   {
      qryGetPaymentTypes->Close();
   }
}

void TPaymentTypeBridge :: Add( TPaymentType* paymentType)
{
   qryAdd->ParamByName( "prmDescription")->AsString = paymentType->description;
   qryAdd->ExecSQL();
}

void TPaymentTypeBridge :: Update( __int64 id, AnsiString newDescription)
{
   qryUpdate->ParamByName( "prmID")->AsInteger = id;
   qryUpdate->ParamByName( "prmDescription")->AsString = newDescription;
   qryUpdate->ExecSQL();
}

void TPaymentTypeBridge :: Delete( __int64 id)
{
   qryDelete->ParamByName( "prmID")->AsInteger = id;
   qryDelete->ExecSQL();
}

bool TPaymentTypeBridge :: InUse( TPaymentType* paymentType)
{
   bool result = false;
   qryInUse->ParamByName( "prmPaymentType")->AsInteger = paymentType->id;

   qryInUse->Open();

   try
   {
      result = (!qryInUse->IsEmpty());
   }
   __finally
   {
      qryInUse->Close();
   }

   return result;
}
